package com.personalforumapi.controller;

import com.personalforumapi.entity.article;
import com.personalforumapi.repository.articleRepostitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Optional;

/**
 * @Author:kinjaze
 * @Date:2020/9/23 16:03
 */
@RestController
@RequestMapping("/detail")
public class articleController {
    @Autowired
    private articleRepostitory articleRepostitory;

    @GetMapping("/findAll")
    public List<article> findAll(){
        return  articleRepostitory.findAll();
    }
    @GetMapping("/findById/{aid}")
    public Optional<article> findById(@PathVariable("aid") Integer aid){
        return articleRepostitory.findById(aid);
    }
    @GetMapping("/findByType/{type}")
    public  List<article> findByType(@PathVariable("type") String type)  {
        return articleRepostitory.findByType(type);
    }
    @GetMapping("/findNew")
    public List<article> findNew(){
        return articleRepostitory.findNew();
    }
    @GetMapping("/findHot")
    public List<article> findHot(){
        return articleRepostitory.findHot();
    }
    @PostMapping("/updateView/{aid}/{view}")
    public void updateView(@PathVariable("aid") Integer aid,@PathVariable("view") Integer view){
        articleRepostitory.updateView(view,aid);
    }
    @PostMapping("/add")
    public boolean add(@RequestBody article article){
        if(articleRepostitory.save(article).equals(article)){
            return true;
        }else{
            return false;
        }
    }
}
